#include "linklist.h"

Linklist::Linklist()
{
    /*创建虚拟头节点*/
    m_head = new LinkNode(0, NULL);
    m_tail = m_head;
    /*链表元素个数为0*/
    m_size = 0;
}

Linklist::~Linklist()
{
    LinkNode *traveNode = m_head;
    while(traveNode != NULL)
    {
        LinkNode *nextNode = traveNode->m_next;
        delete traveNode;
        traveNode = nextNode;
    }
}

/*链表的头插*/
int Linklist::linkListHeadInsert(ELEMENT_TYPE data)
{
    return linkListAppointPosInsert(0, data);
}

/*链表的尾插*/
int Linklist::linkListTailInsert(ELEMENT_TYPE data)
{
    return linkListAppointPosInsert(m_size, data);
}

/*链表任意位置插入*/
int Linklist::linkListAppointPosInsert(int pos, ELEMENT_TYPE data)
{
    if (pos < 0 || pos > m_size)
    {
        cout << "pos error" << endl;
        return -1;
    }

    LinkNode *traveNode = m_head;

    bool isFlag = false;
    if (pos == m_size)
    {
        traveNode = m_tail;
        isFlag = true;
    }
    else
    {
        while (pos)
        {
            traveNode = traveNode->m_next;
            pos--;
        }
    }
    /*退出循环,要插入的位置的前一个位置*/
    LinkNode *newNode = new LinkNode(data, traveNode->m_next);
    traveNode->m_next = newNode;

    if (isFlag == true)
    {
        /*修改尾指针位置*/
        m_tail = newNode;
    }

    (m_size)++;

    return 0;
}

int Linklist::linkListHeadDelete()
{
    return linkListAppointPosDelete(0);
}

int Linklist::linkListTailDelete()
{
    return linkListAppointPosDelete(m_size-1);
}

int Linklist::linkListAppointPosDelete(int pos)
{
    if (pos < 0 || pos >= m_size)
    {
        cout << "pos error" << endl;
        return -1;
    }

    LinkNode *traveNode = m_head;
    bool isFlag = false;
    if(pos ==  m_size-1)
    {
        isFlag = true;
    }
    while (pos)
    {
        traveNode = traveNode->m_next;
        pos--;
    }


    LinkNode * deleteNode = traveNode->m_next;
    /*退出循环,要删除的位置的前一个位置*/
    traveNode->m_next = deleteNode->m_next;
    if(isFlag)
    {
        m_tail = traveNode;  
    } 

    delete deleteNode;
    (m_size)--;
    return 0;
}

LinkNode* Linklist::reverseList()
{
    LinkNode* prev = nullptr;
    LinkNode* current = m_head->m_next;
    LinkNode* nextNode = nullptr;

    while (current != nullptr) {
        nextNode = current->m_next;  
        current->m_next = prev;      
        prev = current;              
        current = nextNode;          
    }

    m_head->m_next = prev; 

    return m_head->m_next;
}

int Linklist::travelArrayElement()
{
    LinkNode *traveNode = m_head->m_next;
    for (int idx = 0; idx < m_size; idx++)
    {
        cout << traveNode->m_data << "\t";
        traveNode = traveNode->m_next;
    }
    cout << endl;
    return 0;
}

/*得到头节点的值*/
ELEMENT_TYPE Linklist::getHeadval()
{
    return linkListGetApponedposData(0);
}





 /*得到尾节点的值*/
ELEMENT_TYPE Linklist::getTailVal()
{
    return linkListGetApponedposData(m_size-1);
}

/*得到任意位置的值*/
ELEMENT_TYPE Linklist::linkListGetApponedposData(int pos)
{
    if (pos < 0 || pos >= m_size)
    {
        cout << "pos error" << endl;
        return NULL;
    }
    LinkNode * traveNode = m_head->m_next;
    bool isFlag = false;
     if (pos == m_size-1)
    {
        traveNode = m_tail;
        isFlag = true;
    }
    else
    {
        while (pos)
        {
            traveNode = traveNode->m_next;
            pos--;
        }
        /*退出循环，travelNode就是要查找位置的元素*/
    }

    return traveNode->m_data;
}

    /*得到链表长度*/
int Linklist::getListLen()
{
    return m_size;
}



// 获取链表的头节点
LinkNode* Linklist::getHeadNode() {
    return m_head->m_next;
}

// 获取链表的尾节点
LinkNode* Linklist::getTailNode() {
    return m_tail;
}

// 更新尾节点
void Linklist::updateTail(LinkNode* newTail) {
    m_tail = newTail;
    m_tail->m_next = m_head;  // 重新连接成循环链表
}

// 减少链表的大小
void Linklist::decreaseSize() {
    m_size--;
}